/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.classloader.observer;

import moved.org.objectweb.asm.ClassReader;
import moved.org.objectweb.asm.ClassVisitor;
import picounit.features.base.exception.CorruptedClassFile;

public class DefaultClassObserver implements ClassObserver {
	private final ClassVisitor classVisitor;
	private final boolean skipDebug;

	private ClassLoader classLoader;

	public DefaultClassObserver(ClassVisitor classVisitor) {
		this(classVisitor, false);
	}

	public DefaultClassObserver(ClassVisitor classVisitor, boolean skipDebug) {
		assert classVisitor != null : "Invalid to use a null ClassVisitor";

		this.classVisitor = classVisitor;
		this.skipDebug = skipDebug;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// ClassObserver
	///////////////////////////////////////////////////////////////////////////////////////////////

	public final void observe(byte[] byteCode) {
		try {
			new ClassReader(byteCode).accept(classVisitor, skipDebug);
		}
		catch (RuntimeException runtimeException) {
			throw new CorruptedClassFile(runtimeException);
		}
	}

	public final void setClassLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation
	///////////////////////////////////////////////////////////////////////////////////////////////

	protected final ClassLoader getClassLoader() {
		return classLoader;
	}
}
